<template>
    <div class="paddingTop search_page">
        <head-top head-title="搜索" goBack="true"></head-top>
        <form class="search_form">
            <input type="search" name="search" placeholder="输入" class="search_input" v-model="searchValue"
                   @input="checkInput">
            <input type="submit" name="submit" class="search_submit" @click.prevent="searchTarget('')">
        </form>
         <div class="goodlist_container"   v-if="goodsList.length" type="1">
             <div class="goodcontainer">
                    <router-link :to="{path: 'good', query:{id: item.id}}" v-for="item in goodsList" class="gooditem">
                            <section>
                                <img :src="item.goods_front_image" class="good_img">
                            </section>
                            <span class="goodtitle">{{item.goods_brief}}</span>
                            <hr style="height:1px;border:none;border-top:0.01em solid #9f9f9ff9;width: 5.7em"/>
                            <span class="goodprice">￥{{item.goods_price}}</span>
                        </router-link>
             </div>
    </div>
<!--        <div class="goodlist_container">-->
<!--            <h5 class="title_goods">全部商品</h5>-->
<!--            <div class="container" v-if="goodsList.length">-->
<!--                <router-link :to="{path: 'good', query:{geohash, id: item.id}}" v-for="item in goodsList" class="good">-->
<!--                    <div class="goodcontainer">-->
<!--                        <section>-->
<!--                            <img :src="item.goods_front_image" class="shop_img">-->
<!--                        </section>-->
<!--                        <span class="goodtitle">{{item.goods_brief}}</span>-->
<!--                        <span class="goodprice">{{item.goods_price}}</span>-->
<!--                    </div>-->
<!--                </router-link>-->
<!--            </div>-->
<!--        </div>-->

    <section class="search_history" v-if="searchHistory.length&&showHistory">
        <h4 class="title_goods">搜索历史</h4>
        <ul>
            <li v-for="(item, index) in searchHistory" :key="index" class="history_list">
                <span class="history_text ellipsis" @click="searchTarget(item)">{{item}}</span>
                <svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="delete_icon" @click="deleteHistory(index)">
                    <line x1="8" y1="8" x2="18" y2="18" style="stroke:#999;stroke-width:3"/>
                    <line x1="18" y1="8" x2="8" y2="18" style="stroke:#999;stroke-width:3"/>
                </svg>
            </li>
        </ul>
        <footer class="clear_history" @click="clearAllHistory">清空搜索历史</footer>
    </section>
    <div class="search_none" v-if="emptyResult">很抱歉！无搜索结果</div>
    <foot-guide></foot-guide>
         </div>
</template>


<script>
    import headTop from '../../components/header/head'
    import footGuide from '../../components/footer/footGuide'
    import {searchGoods} from '../../service/getData'
    import {imgBaseUrl} from '../../config/env'
    import {getStore, setStore} from '../../config/mUtils'
    import goodList from 'src/components/common/goodlist'

    export default {
        data() {
            return {
                geohash: '', // msite页面传递过来的地址信息
                searchValue: '', // 搜索内容
                goodsList: [], // 搜索返回的结果
                imgBaseUrl, // 图片域名地址
                searchHistory: [], // 搜索历史记录
                showHistory: true, // 是否显示历史记录，只有在返回搜索结果后隐藏
                emptyResult: false, // 搜索结果为空时显示
                hasGetData: false,
            }
        },
        created() {

        },
        mounted() {
            this.geohash = this.$route.params.geohash;
            //获取搜索历史记录
            if (getStore('searchHistory')) {
                this.searchHistory = JSON.parse(getStore('searchHistory'));
            }
        },
        components: {
            headTop,
            footGuide,
         //   shopList,
        },
        methods: {
            //点击提交按钮，搜索结果并显示，同时将搜索内容存入历史记录
            async searchTarget(historyValue) {
                if (historyValue) {
                    this.searchValue = historyValue;
                } else if (!this.searchValue) {
                    return
                }
                //隐藏历史记录
                this.showHistory = false;
                //获取搜索结果
                let res = await searchGoods(this.searchValue);
                console.log(res)
                let reslist = res.results
                let i = 0
                for (; i < reslist.length; i++) {
                    this.goodsList[i] = reslist[i].object
                }
                this.emptyResult = !this.goodsList.length;
                /**
                 * 点击搜索结果进入下一页面时进行判断是否已经有一样的历史记录
                 * 如果没有则新增，如果有则不做重复储存，判断完成后进入下一页
                 */
                let history = getStore('searchHistory');
                if (history) {
                    let checkrepeat = false;
                    this.searchHistory = JSON.parse(history);
                    this.searchHistory.forEach(item => {
                        if (item == this.searchValue) {
                            checkrepeat = true;
                        }
                    })
                    if (!checkrepeat) {
                        this.searchHistory.push(this.searchValue)
                    }
                } else {
                    this.searchHistory.push(this.searchValue)
                }
                setStore('searchHistory', this.searchHistory)
            },
            //搜索结束后，删除搜索内容直到为空时清空搜索结果，并显示历史记录
            checkInput() {
                if (this.searchValue === '') {
                    this.showHistory = true; //显示历史记录
                    this.goodsList = []; //清空搜索结果
                    this.emptyResult = false; //隐藏搜索为空提示
                }
            },
            //点击删除按钮，删除当前历史记录
            deleteHistory(index) {
                this.searchHistory.splice(index, 1);
                setStore('searchHistory', this.searchHistory);
            },
            //清除所有历史记录
            clearAllHistory() {
                this.searchHistory = [];
                setStore('searchHistory', this.searchHistory);
            }
        }
    }

</script>

<style lang="scss" scoped>
    @import '../../style/mixin';

    .search_page {
        margin-bottom: 2rem;
    }

    .search_form {
        background-color: #fff;
        padding: 0.5rem;
        display: flex;

        input {
            height: 1.5rem;
        }

        .search_input {
            flex: 4;
            border: 0.025rem solid $bc;
            @include sc(0.65rem, #333);
            border-radius: 0.125rem;
            background-color: #f2f2f2;
            font-weight: bold;
            padding: 0 0.25rem;
        }

        .search_submit {
            flex: 1;
            border: 0.025rem solid $blue;
            margin-left: .2rem;
            @include sc(0.65rem, #fff);
            border-radius: 0.125rem;
            background-color: $blue;
            font-weight: bold;
            padding: 0 0.25rem;
        }
    }

    .title_goods {
        font-size: 0.6rem;
        line-height: 1.5rem;
        text-indent: 0.5rem;
        font-weight: bold;
        color: #666;
        background-color: #f8f9fa
    }

    .list_container {
        background-color: #fff;
    }

    .list_li {
        display: flex;
        justify-content: 'center';
        padding: 0.5rem;
        border-bottom: 0.025rem solid $bc;

        .item_left {
            margin-right: 0.25rem;

            .restaurant_img {
                @include wh(1.7rem, 1.7rem);
            }
        }

        .item_right {
            font-size: 0.55rem;
            flex: 1;

            .item_right_text {
                padding-bottom: 0.25rem;
                border-bottom: 0.025rem solid $bc;

                p {
                    line-height: .9rem;
                }

                .pay_icon {
                    margin-bottom: -0.08rem;
                }
            }

            .item_right_detail {
                margin-top: 0.25rem;

                li {
                    font-size: 0;

                    span {
                        font-size: .5rem;
                        vertical-align: middle;
                        display: inline-block;
                        margin-bottom: 0.2rem;
                    }

                    .activities_icon {
                        @include sc(.5rem, #fff);
                        font-weight: bold;
                        padding: .04rem;
                        border-radius: 0.15rem;
                        margin-right: 0.125rem;
                    }

                    .only_phone {
                        color: #FF6000;
                    }
                }
            }
        }
    }

    .search_history {
        .history_list {
            background-color: #fff;
            border-bottom: 0.025rem solid $bc;
            @include font(0.7rem, 2rem);
            padding: 0 0.3rem;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .history_text {
                display: inline-block;
                width: 80%;
            }

            .delete_icon {
                @include wh(1rem, 1rem);
            }
        }

        .clear_history {
            background-color: #fff;
            color: $blue;
            @include font(.7rem, 2rem);
            font-weight: bold;
            text-align: center;
        }
    }

    .search_none {
        margin: 0 auto;
        @include font(0.65rem, 1.75rem);
        color: #333;
        background-color: #fff;
        text-align: center;
        margin-top: 0.125rem;
    }

    .goodlist_container {
        background-color: #f5f5f5;
        margin-bottom: 2rem;
        display: block;
    }

    .goodcontainer {
        background-color: #f5f5f5;
        margin: 0.6em;
        border-radius: 0.2em;
        overflow: hidden;
        width: 15.4em;
        //float: left;
        // font-size: 0;

        .goodtitle {
            width: 100%;
            float: left;
            color: #333;
            //word-break:normal;
            word-wrap: break-word;
            // padding-top: .01rem;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: normal;
            margin-top: 0.2rem;
            @include font(0.4rem, 0.5rem, 'PingFangSC-Regular');
            font-weight: 700;
            text-align: left;
            display: block;
            //vertical-align: top;
        }

        hr {
            height: 0.01em;
            border: none;
            border-top: 0.01em solid #9f9f9ff9;
            width: 5.7em;
            display: block;
            margin-top: 0.4em;
            margin-bottom: 0.3em;
            float: left;
        }

        .goodprice {
            width: 50%;
            display: block;
            transform: scale(.5);
            margin-top: 0em;
            display: block;
            float: right;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: normal;

            .supports {
                @include sc(0.3rem, #999);
                border: 0.025rem solid #f1f1f1;
                padding: 0 0.04rem;
                border-radius: 0.08rem;
                margin-left: 2rem;
            }
        }
    }

    .good_img {
        //@include wh(2.7rem, 2.7rem);
        display: block;
        margin-right: 0.4rem;
        height: 6rem;
        width: 100%;
    }

     .gooditem{
        display: inline-block;
        //float: left;
        width: 7.2em;
        margin: 0.1em;
        /*margin-right: 0.45em;*/
        /*margin-bottom: 0.3em;*/
        border-radius: 0.2em;
        background-color: #fff;
        overflow: hidden;
    }
</style>
